<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\Pic;
use app\admin\model\Version;
use app\admin\model\XcxAudit;
use app\admin\model\Item;
use app\admin\model\Miaosha;
use app\admin\model\NewPeopleToItem;
use app\admin\model\Overgroup;
use app\admin\model\Notice;
use app\admin\model\Daping;
use app\admin\model\Opinion;
use app\admin\model\Custom;
use app\admin\model\Staff;
use app\admin\model\AppAudit;

class SystemController extends CommonController {
    /*
     * 图片管理
     * author xfwed 
     * date 2019.09.21
     */

    public function imgList() {
        $type = input('param.type');
        $pro_type = input('param.pro_type');
        $perPage = input('param.perPage');
        $page = input('param.page');

        if ($type) {
            $where['type'] = $type;
        }

        if ($pro_type) {
            $where['pro_type'] = $pro_type;
        }
        $where['isshow'] = 1;
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $list = Pic::where($where)->order('id desc')->paginate($perPage)->toArray();
        foreach ($list['data'] as $k => $v) {
            $imgClassList = $this->imgClassList();
            $imgClassList = json_decode($imgClassList, true)['data'];
            foreach ($imgClassList as $key => $val) {
                if ($val['id'] == $v['type']) {
                    $list['data'][$k]['typeName'] = $val['name'];
                }
            }
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 图片分类
     * author xfwed 
     * date 2019.09.21
     */

    public function imgClassList() {//1：首页，2超值套餐,3限量抢购，4=99特权
        $list = [
            [
                'id' => 1,
                'name' => '小程序首页轮播图'
            ],
            [
                'id' => 2,
                'name' => '超值组合'
            ],
            [
                'id' => 3,
                'name' => '限量抢购'
            ],
            [
                'id' => 4,
                'name' => '99特权'
            ],
            [
                'id' => 5,
                'name' => 'APP启动图'
            ],
            [
                'id' => 6,
                'name' => 'APP首页icon'
            ],
            [
                'id' => 7,
                'name' => 'APP轮播图'
            ],
            [
                'id' => 8,
                'name' => '智慧前台广告轮播'
            ],
            [
                'id' => 9,
                'name' => '小程序抢购列表广告图'
            ],
            [
                'id' => 10,
                'name' => '小程序我的页面广告图'
            ],
            [
                'id' => 11,
                'name' => '首页通栏'
            ],
            [
                'id' => 12,
                'name' => '评价广告'
            ],
            [
                'id' => 13,
                'name' => '心服务广告'
            ],
            [
                'id' => 14,
                'name' => '预约服务广告'
            ],
            [
                'id' => 15,
                'name' => '热销榜'
            ],
            [
                'id' => 16,
                'name' => '智慧前台首页轮播'
            ],
        ];
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 添加图片
     * author xfwed 
     * date 2019.09.21
     */

    public function addImg() {
        $pic = input('param.pic');
        $pro_type = input('param.pro_type');
        $type = input('param.type');
        $url = input('param.url');
        $note = input('param.note');
        $tone = input('param.tone');
        $pic2 = input('param.pic2');
        $sort = input('param.sort');
        $city_name = input('param.city_name');//可显示的城市名称

        $data = [
            'pic' => $pic,
            'pic2' => $pic2,
            'type' => $type,
            'pro_type' => $pro_type,
            'url' => $url,
            'note' => $note,
            'tone' => $tone,
            'sort' => $sort,
            'add_time' => date('Y-m-d H:i:s'),
            'city_name' => $city_name,
        ];
        $add = Pic::insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 修改图片
     * author xfwed 
     * date 2019.10.18
     */

    public function saveImg() {
        $requestType = input('param.requestType');
        $id = input('param.id');
        if ($requestType == 1) {//1修改前获取信息 2提交修改
            $imgRow = Pic::where('id', '=', $id)->find();
            return $this->ajaxReturn(0, '查询成功', $imgRow);
        } else {
            $pic = input('param.pic');
            $pro_type = input('param.pro_type');
            $type = input('param.type');
            $url = input('param.url');
            $note = input('param.note');
            $tone = input('param.tone');
            $pic2 = input('param.pic2');
            $sort = input('param.sort');
            $city_name = input('param.city_name');//可显示的城市名称

            $data = [
                'pic' => $pic,
                'pic2' => $pic2,
                'type' => $type,
                'pro_type' => $pro_type,
                'url' => $url,
                'note' => $note,
                'tone' => $tone,
                'sort' => $sort,
                'city_name' => $city_name,
            ];
            $add = Pic::where('id', $id)->update($data);
            if ($add) {
                return $this->ajaxReturn(0, '修改成功', null);
            } else {
                return $this->ajaxReturn(3, '修改失败', null);
            }
        }
    }

    /*
     * 删除图片
     * author xfwed 
     * date 2019.09.21
     */

    public function delImg() {
        $id = input('param.id');

        $data = [
            'isshow' => 2,
        ];
        $save = Pic::where('id', '=', $id)->update($data);
        if ($save) {
            return $this->ajaxReturn(0, '删除成功', null);
        } else {
            return $this->ajaxReturn(3, '删除失败', null);
        }
    }

    /*
     * 版本更新历史记录
     * author xfwed 
     * date 2019.09.21
     */

    public function versionList() {
        $list = Version::order('addtime desc')->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 更新版本
     * author xfwed 
     * date 2019.09.21
     */

    public function addVersion() {
        $android_version = input('param.android_version');
        $android_content = input('param.android_content');
        $ios_version = input('param.ios_version');
        $ios_content = input('param.ios_content');
        $wxapp_version = input('param.wxapp_version');
        $wxapp_content = input('param.wxapp_content');
        $force_update = input('param.force_update');

        $data = [
            'android_version' => $android_version,
            'android_content' => $android_content,
            'ios_version' => $ios_version,
            'ios_content' => $ios_content,
            'wxapp_version' => $wxapp_version,
            'wxapp_content' => $wxapp_content,
            'force_update' => $force_update,
            'addtime' => date('Y-m-d H:i:s'),
        ];
        $add = Version::insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '更新成功', null);
        } else {
            return $this->ajaxReturn(3, '更新失败', null);
        }
    }

    /*
     * 小程序审核隐藏项目
     * author xfwed 
     * date 2020.01.07
     */

    public function xcxAudit() {
        $is_open = input('param.is_open'); //1是2否 打开	
        $version = input('param.version'); //版本号	
        $item_ids = input('param.item_ids'); //需要删除的项目id	
        $miaosha_ids = input('param.miaosha_ids'); //需要删除的秒杀id	
        $overgroup_ids = input('param.overgroup_ids'); //需要删除的组合id	
        $newpeopleitem_ids = input('param.newpeopleitem_ids'); //需要删除的新人专享id	
        $good_ids = input('param.good_ids'); //需要删除的商品id
        $type = input('param.type'); //1显示 2修改
        $row = XcxAudit::find();
        $data['info'] = $row;
        if ($type == 1) {
            //项目
            $itemList = Item::where('is_del', 1)->field('id,item_name')->select();
            $item_ids = explode(',', $row['item_ids']);
            foreach ($itemList as $k => $v) {
                if (in_array($v['id'], $item_ids)) {
                    $itemList[$k]['is_select'] = 1;
                } else {
                    $itemList[$k]['is_select'] = 2;
                }
            }
            $data['itemList'] = $itemList;
            //秒杀
            $miaoshaList = Miaosha::where('is_del', 1)->field('id,name')->select();
            $miaosha_ids = explode(',', $row['miaosha_ids']);
            foreach ($miaoshaList as $k => $v) {
                if (in_array($v['id'], $miaosha_ids)) {
                    $miaoshaList[$k]['is_select'] = 1;
                } else {
                    $miaoshaList[$k]['is_select'] = 2;
                }
            }
            $data['miaoshaList'] = $miaoshaList;
            //组合
            $overgroupList = Overgroup::where('status', 1)->field('id,name')->select();
            $overgroup_ids = explode(',', $row['overgroup_ids']);
            foreach ($overgroupList as $k => $v) {
                if (in_array($v['id'], $overgroup_ids)) {
                    $overgroupList[$k]['is_select'] = 1;
                } else {
                    $overgroupList[$k]['is_select'] = 2;
                }
            }
            $data['overgroupList'] = $overgroupList;
            //新人专享
            $newpeopleitemList = NewPeopleToItem::alias('n')
                    ->join('item i', 'i.id = n.item_id', 'left')
                    ->where('n.is_del', 1)
                    ->field('n.id,i.item_name')
                    ->select();
            $newpeopleitem_ids = explode(',', $row['newpeopleitem_ids']);
            foreach ($newpeopleitemList as $k => $v) {
                if (in_array($v['id'], $newpeopleitem_ids)) {
                    $newpeopleitemList[$k]['is_select'] = 1;
                } else {
                    $newpeopleitemList[$k]['is_select'] = 2;
                }
            }
            $data['newpeopleitemList'] = $newpeopleitemList;
            //商品
            $goodList = Db::table('goods_info')
                    ->where('verifyStatus', 2)
                    ->where('valid', 1)
                    ->field('id,name')
                    ->select();
            $good_ids = explode(',', $row['good_ids']);
            foreach ($goodList as $k => $v) {
                if (in_array($v['id'], $good_ids)) {
                    $goodList[$k]['is_select'] = 1;
                } else {
                    $goodList[$k]['is_select'] = 2;
                }
            }
            $data['goodList'] = $goodList;
            return $this->ajaxReturn(0, '查询成功', $data);
        } elseif ($type == 2) {
            $save = XcxAudit::where('id', 1)
                    ->update([
                'is_open' => $is_open,
                'version' => $version,
                'item_ids' => $item_ids,
                'miaosha_ids' => $miaosha_ids,
                'overgroup_ids' => $overgroup_ids,
                'newpeopleitem_ids' => $newpeopleitem_ids,
                'good_ids' => $good_ids,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
            if ($save) {
                return $this->ajaxReturn(0, '修改成功', null);
            } else {
                return $this->ajaxReturn(3, '修改失败', null);
            }
        }
    }

    /*
     * 添加公告
     * author xfwed 
     * date 2020.04.08
     */

    public function addNotice() {
        $title = input('param.title');
        $author = input('param.author');
        $listimg = input('param.listimg');
        $content = input('param.content');
        $issue = input('param.issue');
        $type = input('param.type');

        $data = [
            'title' => $title,
            'author' => $author,
            'listimg' => $listimg,
            'content' => $content,
            'issue' => $issue,
            'type' => $type,
            'add_time' => date('Y-m-d H:i:s'),
        ];
        $add = Notice::insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 修改公告
     * author xfwed 
     * date 2020.04.08
     */

    public function saveNotice() {
        $requestType = input('param.requestType');
        $id = input('param.id');
        if ($requestType == 1) {//1修改前获取信息 2提交修改
            $imgRow = Notice::where('id', '=', $id)->find();
            return $this->ajaxReturn(0, '查询成功', $imgRow);
        } else {
            $title = input('param.title');
            $author = input('param.author');
            $listimg = input('param.listimg');
            $content = input('param.content');
            $issue = input('param.issue');
            $type = input('param.type');

            $data = [
                'title' => $title,
                'author' => $author,
                'listimg' => $listimg,
                'content' => $content,
                'issue' => $issue,
                'type' => $type,
                'add_time' => date('Y-m-d H:i:s'),
            ];
            $add = Notice::where('id', $id)->update($data);
            if ($add) {
                return $this->ajaxReturn(0, '修改成功', null);
            } else {
                return $this->ajaxReturn(3, '修改失败', null);
            }
        }
    }

    /*
     * 公告列表
     * author xfwed 
     * date 2020.04.08
     */

    public function noticeList() {
        $type = input('param.type');
        $issue = input('param.issue');
        $title = input('param.title');
        $perPage = input('param.perPage');
        $page = input('param.page');

        if ($type) {
            $where['type'] = $type;
        }

        if ($issue) {
            $where['issue'] = $issue;
        }

        if ($title) {
            $where['title'] = ['like', '%' . $title . '%'];
        }
        $where = isset($where) ? $where : [];
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $list = Notice::where($where)
                ->field('id,title,author,listimg,issue,type,visit_num,add_time')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 公告发布/下架
     * author xfwed 
     * date 2020.04.08
     */

    public function upperNotice() {
        $post = input('param.');
        $rule = [
            'id' => 'require',
            'issue' => 'require|integer',
        ];
        $msg = [
            'id.require' => '请选择公告',
            'issue.require' => '请选择发布或者下架',
            'issue.integer' => '发布或者下架编号为数字',
        ];
        $validate = new Validate($rule, $msg);
        if (!$validate->check($post)) {
            $error = $validate->getError();

            $this->ajaxReturn(1, $error, null);
        }
        $data = [
            'issue' => $post['issue']
        ];

        Notice::where('id', '=', $post['id'])->update($data);
        if ($post['issue'] == 1) {
            $msg = '发布成功';
        } else {
            $msg = '下架成功';
        }
        return $this->ajaxReturn(0, $msg, null);
    }

    /*
     * 数据大屏
     * author xfwed 
     * date 2020.06.22
     */

    public function daPing() {
        $post = input('param.');
        if ($post['type'] == 1) {//type 1回显数据  2新增数据
            $row = Daping::order('id desc')->find();
            return $this->ajaxReturn(0, '查询成功', $row);
        } else {
            $data['leftTop1'] = $post['leftTop1'];
            $data['leftTop2'] = $post['leftTop2'];
            $data['leftTop3'] = $post['leftTop3'];
            $data['leftTop4'] = $post['leftTop4'];
            $data['leftTop5'] = $post['leftTop5'];
            $data['leftTop6'] = $post['leftTop6'];
            $data['leftTop7'] = $post['leftTop7'];
            $data['leftTop8'] = $post['leftTop8'];
            $data['leftTop9'] = $post['leftTop9'];
            $data['leftTop10'] = $post['leftTop10'];
            $data['rightTop1'] = $post['rightTop1'];
            $data['rightTop2'] = $post['rightTop2'];
            $data['rightTop3'] = $post['rightTop3'];
            $data['rightTop4'] = $post['rightTop4'];
            $data['rightTop5'] = $post['rightTop5'];
            $data['rightTop6'] = $post['rightTop6'];
            $data['rightTop7'] = $post['rightTop7'];
            $data['rightTop8'] = $post['rightTop8'];
            $data['rightTop9'] = $post['rightTop9'];
            $data['rightTop10'] = $post['rightTop10'];
            $data['rightDown1'] = $post['rightDown1'];
            $data['rightDown2'] = $post['rightDown2'];
            $data['rightDown3'] = $post['rightDown3'];
            $data['rightDown4'] = $post['rightDown4'];
            $result = Daping::insert($data);
            return $this->ajaxReturn(0, '添加成功', null);
        }
    }

    /*
     * 用户吐槽
     * author xfwed 
     * date 2020.06.29
     */

    public function opinionList() {
        $type = input('param.type');
        $status = input('param.status');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $phone = input('param.phone');

        if ($type) {
            $where['type'] = $type;
        }
        if($status){
            $where['status'] = $status;
        }
        if($type == 1){
            if($phone){
                $ids = Custom::where('custom_phone',$phone)->column('id');
                $where['userid'] = ['in',$ids];
            }
            $a = '';
        }elseif ($type == 2 || $type == 3) {
            if($phone){
                $ids = Staff::where('telephone',$phone)->column('id');
                $where['userid'] = ['in',$ids];
            }
            $a = '';
        }else{
            if($phone){
                $cids = Custom::where('custom_phone',$phone)->column('id');
                $sids = Staff::where('telephone',$phone)->column('id');
                $cids = implode(',', $cids);
                $sids = implode(',', $sids);
                if($cids){
                    if($sids){
                        $a = "(userid in (".$cids.") and type = 1) or (userid in (".$sids.") and type in(2,3))";
                    }  else {
                        $a = "userid in (".$cids.") and type = 1";
                    }
                }  else {
                    if($sids){
                        $a = "userid in (".$sids.") and type in(2,3)";
                    }  else {
                        $a = '';
                    }
                }
                
            }  else {
                $a = '';
            }
        }
        
        $where = isset($where) ? $where : [];
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $list = Opinion::where($where)
                ->where($a)
                ->order('add_time desc')
                ->paginate($perPage)
                ->each(function($item,$key){
                    if($item['type'] == 1){
                        $row = Custom::where('id',$item['userid'])->field('custom_phone,custom_name,longitude,latitude')->find();
                        $item['name'] = $row['custom_name'];
                        $item['phone'] = $row['custom_phone'];
                        $item['longitude'] = $row['longitude'];
                        $item['latitude'] = $row['latitude'];
                        $item['cityName'] = '未知';
                    }  else {
                        $row = Staff::where('id',$item['userid'])->field('telephone,staff_name,longitude,latitude,city')->find();
                        $item['name'] = $row['staff_name'];
                        $item['phone'] = $row['telephone'];
                        $item['longitude'] = $row['longitude'];
                        $item['latitude'] = $row['latitude'];
                        $item['cityName'] = Db::name('prov_city_area')->where('id',$row['city'])->value('merger_name');
                    }
                });
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 处理用户吐槽
     * author xfwed 
     * date 2020.10.16
     */

    public function dealOpinion() {
        $id = input('param.id');
        $deal_name = input('param.deal_name');
        $deal_result = input('param.deal_result');
        $opinionRow = Opinion::where('id',$id)->find();
        if($opinionRow['status'] == 1){
            return $this->ajaxReturn(3, '已经处理了，不需要重复处理', null);
        }
        $result = Opinion::where('id',$id)
                ->update([
                    'deal_name' => $deal_name,
                    'deal_time' => date('Y-m-d H:i:s'),
                    'deal_result' => $deal_result,
                    'status' => 1,
                ]);
        return $this->ajaxReturn(0, '处理成功', $result);
    }
    /*
     * app请求地址
     * author xfwed 
     * date 2020.07.13
     */

    public function appAudit() {
        $is_open = input('param.is_open'); //1是2否 打开	
        $version = input('param.version'); //版本号	
        $type = input('param.type'); //1显示 2修改
        $row = AppAudit::find();
        if ($type == 1) {
            return $this->ajaxReturn(0, '查询成功', $row);
        } elseif ($type == 2) {
            $save = AppAudit::where('id', 1)
                    ->update([
                'is_open' => $is_open,
                'version' => $version,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
            if ($save) {
                return $this->ajaxReturn(0, '修改成功', null);
            } else {
                return $this->ajaxReturn(3, '修改失败', null);
            }
        }
    }

}
